package cn.myfirefly.moming.common.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.myfirefly.moming.common.dao.mapper.DataDictMapper;
import cn.myfirefly.moming.common.entity.MMDataDict;
import cn.myfirefly.moming.common.model.DataDictDto;
import cn.myfirefly.moming.common.model.DataDictFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.mybatis.BjuiDataGridFilter;
import cn.myfirefly.core.mybatis.BjuiDataGridSqlHelper;

@Repository
public class DataDictDao extends BaseDao<MMDataDict> {
	
	@Autowired
	private DataDictMapper dataDictMapper;
	
	public PageResultDto<DataDictDto> getList(DataDictFilterDto dto) {
		PageResultDto<DataDictDto> resultDto = new PageResultDto<>();
		//参数处理
		Map<String, Object> paraMap = new HashMap<>();
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto);
		paraMap.putAll(bjuiDataGridFilter.getParam());
		//分页处理
		Page<DataDictDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<DataDictDto> list = dataDictMapper.getList(paraMap);
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		return resultDto;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<MMDataDict> getListByDataType(String dataType) {
		Session session = getCurrentSession();
		Query query = session.createQuery("from MMDataDict as d where d.dataType=? order by d.createDate asc ");
		query.setString(0, dataType);
		List list = (List)query.list();
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}
}
